package flex.cc.nursing.util;

import java.security.GeneralSecurityException;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import com.google.common.io.BaseEncoding;

import flex.cc.common.utils.DateUtil;
import flex.cc.nursing.dto.vo.AppointmentRequestDTO;
import flex.cc.nursing.dto.vo.Request;
import flex.cc.nursing.dto.vo.RequestHead;

/**
 * 大树保签名生成工具类
 * @author R
 */
public class SignUtil {
	private static final Logger logger = LoggerFactory.getLogger(SignUtil.class);
	private static final String AES_ALG = "AES";
	private static final String HMACSHA1_ALG = "HmacSHA1";
	
	
	/**
	 * 生成签名
	 * @param requestJsonStr  请求报文
	 * @param signKey  签名key
	 * @return  签名
	 */
	public static String generateSign(String content,String signKey){
		
        content = signKey + content;
        logger.info("【签名数据】{}", content);
        byte[] bytes1 = hmacSha1(content.getBytes(Charsets.UTF_8), decodeHex(signKey));
        String sign = encodeBase64(bytes1);
        logger.info("【签名】{}", sign);
		
		return sign;
	}
	
	
	/**
	 * 使用AES加密原始字符串.
	 *
	 * @param input 原始输入字符数组
	 * @param key   符合AES要求的密钥
	 * @see CryptoUtil#generateAesKey()
	 */
	public static byte[] aesEncrypt(byte[] input, byte[] key) {
	    return aes(input, key, Cipher.ENCRYPT_MODE);
	}

	/**
	 * 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
	 *
	 * @param input 原始字节数组
	 * @param key   符合AES要求的密钥
	 * @param mode  Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
	 */
	private static byte[] aes(byte[] input, byte[] key, int mode) {
	    try {
	        SecretKey secretKey = new SecretKeySpec(key, AES_ALG);
	        Cipher cipher = Cipher.getInstance(AES_ALG);
	        cipher.init(mode, secretKey);
	        return cipher.doFinal(input);
	    } catch (GeneralSecurityException e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
	public static byte[] hmacSha1(byte[] input, byte[] key) {
	    try {
	        SecretKey secretKey = new SecretKeySpec(key, HMACSHA1_ALG);
	        Mac mac = Mac.getInstance(HMACSHA1_ALG);
	        mac.init(secretKey);
	        return mac.doFinal(input);
	    } catch (GeneralSecurityException e) {
	        e.printStackTrace();
	    }
	    return null;
	}

	/**
	 * Base64编码. 见 RFC4648
	 */
	public static byte[] decodeBase64(String src) {
	    return Base64.getDecoder().decode(src);
	}
	
	/**
	 * Base64编码. 见 RFC4648
	 */
	public static String encodeBase64(byte[] input) {
	    return Base64.getEncoder().encodeToString(input);
	}

	/**
	 * Hex解码, 将String解码为byte[].
	 * <p>
	 * 字符串有异常时抛出IllegalArgumentException.
	 */
	public static byte[] decodeHex(CharSequence input) {
	    return BaseEncoding.base16().decode(input);
	}
}
